<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Facebook extends Public_Controller
{
	public function __construct()
	{
		parent::__construct();
		// Load all the required classes
		$this->load->library('form_validation');
		$this->load->model('stoepje_winners_m');
		$this->load->model('stoepje_vouchers_m');
		$this->load->model('stoepje_prizes_m');
		$this->load->model('pages_m');
		$this->load->model('page_chunks_m');
		$this->lang->load('voucher', 'dutch');
		$this->load->helper('captcha');
		$this->config->load('voucher');
		$this->_folder = $this->config->item('upload_image_path');
		$this->_path = FCPATH . $this->config->item('upload_image_path');
		// Set the validation rules
	}

	/**
	 * List all items
	 */
	public function getvoucher()
	{
		$result = array();
		$code = $this->input->post('code');		
		$voucher = $this->stoepje_vouchers_m->where('status', 0)->get_by('code', $code);
		if($voucher){
			$result['return'] = true;
			$result['voucher'] = $voucher;
		}else{
			$result['return'] = false;
		}
		echo json_encode($result);
	}
	public function addwinner()
	{
		$result = array();
		$code = $this->input->post('code');
		$salutation = $this->input->post('salutation');
		$firstName = $this->input->post('first_name');
		$lastName = $this->input->post('last_name');
		$postcode = $this->input->post('postcode');
		$email = $this->input->post('email');
		
		$voucher = $this->stoepje_vouchers_m->where('status', 0)->get_by('code', $code);
		$prize = $this->stoepje_prizes_m->get($voucher->prize_id);
		$this->stoepje_winners_m->insert(array(
					'code'				=> $code,
					'salutation'		=> $salutation,
					'first_name'		=> $firstName,
					'last_name'			=> $lastName,
					'postcode'			=> $postcode,
					'email'				=> $email,
					'prize_id'			=> $prize->id,
					'prize_name'		=> $prize->name,
					'ip'				=> $this->input->ip_address(),
					'submit_time'		=> time()
				));
				
		$this->stoepje_vouchers_m->update($voucher->id, array(
			'status' 		=> 1
		));
		
		$this->load->library('settings');
		$this->load->library('email');
		$this->email->from($sendEmail = $this->settings->get('server_email'), lang('stoepje_winner_frontend_email_name_label'));
		$this->email->to($email);
		//$this->email->cc('another@another-example.com');
		//$this->email->bcc('them@their-example.com');
		$this->email->subject(lang('stoepje_winner_frontend_email_subject_label'));
		$prize->email = str_replace('###SALUTATION###', lang('stoepje_winner_salutation_'.$salutation), $prize->email);
		$prize->email = str_replace('###FIRSTNAME###', $firstName, $prize->email);
		$prize->email = str_replace('###LASTNAME###', $lastName, $prize->email);
		$prize->email = str_replace('###VOUCHERCODE###', $voucher->code, $prize->email);
		$this->email->message($prize->email);
		if($prize->id != 7){
			$this->email->send();
		}
		
		$result['status'] = true;
		echo json_encode($result);
	}
	
	
	public function submit()
	{
		/*
		$captcha = $this->input->post('captcha');
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		*/
		$code = $this->input->post('code');
		$salutation = $this->input->post('salutation');
		$firstName = $this->input->post('first_name');
		$lastName = $this->input->post('last_name');
		$postcode = $this->input->post('postcode');
		$email = $this->input->post('email');
		$accept = $this->input->post('accept');
		
		$this->load->helper('email');
		
		$errorMessage = '';
		$oneEurePrize = false;
		$voucher = null;
		$prize = null;
		/*
		if( $correctCaptcha != $captcha){
			$errorMessage = lang('stoepje_winner_frontend_code_invalid_captcha');
		}else if( time() > $expTime ){
			$errorMessage = lang('stoepje_winner_frontend_code_captcha_expired');
		}else 
		*/
		if($code == ""){
			$errorMessage = lang('stoepje_winner_frontend_code_required_error');
		}else if($firstName == ""){
			$errorMessage = lang('stoepje_winner_frontend_first_name_required_error');
		}else if($lastName == ""){
			$errorMessage = lang('stoepje_winner_frontend_last_name_required_error');
		}else if($postcode == ""){
			$errorMessage = lang('stoepje_winner_frontend_postcode_required_error');
		}else if($email == ""){
			$errorMessage = lang('stoepje_winner_frontend_email_required_error');
		}else if(!valid_email($email)){
			$errorMessage = lang('stoepje_winner_frontend_email_invalid_error');
		}else if(!$accept){
			$errorMessage = lang('stoepje_winner_frontend_accept_required_error');
		}
		if(!$errorMessage){
			$voucher = $this->stoepje_vouchers_m->where('status', 0)->get_by('code', $code);
			if($voucher){
				$prize = $this->stoepje_prizes_m->get($voucher->prize_id);
				if($prize){
					$this->stoepje_winners_m->insert(array(
						'code'				=> $code,
						'salutation'		=> $salutation,
						'first_name'		=> $firstName,
						'last_name'			=> $lastName,
						'postcode'			=> $postcode,
						'email'				=> $email,
						'prize_id'			=> $prize->id,
						'prize_name'		=> $prize->name,
						'ip'				=> $this->input->ip_address(),
						'submit_time'		=> time()
					));
					
					$this->stoepje_vouchers_m->update($voucher->id, array(
						'status' 		=> 1
					));
					
					$this->load->library('settings');
					$this->load->library('email');
					$this->email->from($sendEmail = $this->settings->get('server_email'), lang('stoepje_winner_frontend_email_name_label'));
					$this->email->to($email);
					//$this->email->cc('another@another-example.com');
					//$this->email->bcc('them@their-example.com');
					$this->email->subject(lang('stoepje_winner_frontend_email_subject_label'));
					$prize->email = str_replace('###SALUTATION###', lang('stoepje_winner_salutation_'.$salutation), $prize->email);
					$prize->email = str_replace('###FIRSTNAME###', $firstName, $prize->email);
					$prize->email = str_replace('###LASTNAME###', $lastName, $prize->email);
					$prize->email = str_replace('###VOUCHERCODE###', $voucher->code, $prize->email);
					$this->email->message($prize->email);
					if($prize->id != 7){
						$this->email->send();
					}
				}else{
					$oneEurePrize = true;
				}
			}else{
				$errorMessage = lang('stoepje_winner_frontend_no_existed_code_error');
			}
		}
		
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		$this->data->imagePath = $this->_path;
		$this->data->imageFolder = $this->_folder;
		$this->data->errorMessage = $errorMessage;
		$this->data->oneEurePrize = $oneEurePrize;
		$this->data->voucher = $voucher;
		$this->data->prize = $prize;
		$content = $this->load->view('result', $this->data, TRUE);

		return $this->template->build_json(array(
			'status'	=> $errorMessage? false: true,
			'content'	=> $content
		));
		/*
		$this->template
			->set('imagePath', $this->_path)
			->set('imageFolder', $this->_folder)
			->set('errorMessage', $errorMessage)
			->set('oneEurePrize', $oneEurePrize)
			->set('voucher', $voucher)
			->set('prize', $prize)
			->build('result', $this->data);
		*/
	}
	
	private function _rand_string($length = 5)
	{
		$chars = 'ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz';
		$max = strlen($chars)-1;
		$string = '';
		mt_srand((double)microtime() * 1000000);
		while (strlen($string) < $length)
		{
			$string .= $chars{mt_rand(0, $max)};
		}
		return $string;
	}
	
	public function term_of_use(){
		$page = $this->pages_m->get_by('slug', 'term-of-use');
		$pageChunks = $this->page_chunks_m->order_by('sort', 'asc')->get_many_by('page_id', $page->id);
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		$this->template
			->set('pageChunks', $pageChunks)
			->build('term_of_use', $this->data);
	}
	public function refesh_captcha(){
		$captchaWord = $this->_rand_string();
		$vals = array(
		    'word' => $captchaWord,
		    'img_path' => './uploads/default/captcha/',
		    'img_url' => site_url().'uploads/default/captcha/',
		    'font_path' => './uploads/default/captcha/fonts/TimesNewRomanBold.ttf',
		    'img_width' => '150',
		    'img_height' => 30,
		    'expiration' => 7200
		    );
		$timeLIMIT = 720;
		$captcha = array( 'captcha_exprise_time'=>time() + $timeLIMIT , 'captcha_content'=>$captchaWord );
		$ci = &get_instance();
		$ci->session->set_userdata($captcha);
		$cap = create_captcha($vals);
		
		$result = array();
		$result['result'] = true;
		$result['data'] = $cap['image'];
		echo json_encode($result);
	}
}
